package com.ztf.until;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionManager {

    //数据库驱动
    private static final String DRIVER = "com.mysql.jdbc.Driver";

    //数据库连接地址
    private static final String URL = "jdbc:mysql://localhost:3306/customerService";

    //数据库连接账号
    private static final String USER = "root";

    //数据库连接密码
    private static final String PASSWORD = "root";


    /**
     * 拒绝实例化
     */
    private ConnectionManager(){

    }



    /**
     * 获取数据库连接对象
     * @return
     */
    public static Connection getConnection(){
        Connection conn = null;
        try {
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(URL,USER,PASSWORD);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }



    /**
     * 关闭数据库
     * @param list
     */
    public static void closeConnection(AutoCloseable...list){
        for (AutoCloseable autoCloseable : list) {
            if (autoCloseable!=null){
                try {
                    autoCloseable.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
